home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / Glypha III Src Folder / Code ƒ / Sound.c < prev    next >
Encoding:
Text File  |  1995-01-30  |  6.9 KB  |  306 lines  |  [TEXT/MPCC]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //                                    Sound.c
  5. //----------------------------------------------------------------------------
  6. //============================================================================
  7.  
  8.  
  9. #include <Sound.h>
  10. #include "Externs.h"
  11.  
  12.  
  13. #define kMaxSounds                17            // number of sounds to load
  14. #define    kBaseBufferSoundID        1000        // ID of first sound (assumed sequential)
  15. #define kSoundDone                913
  16. #define kSoundDone2                749
  17.  
  18.  
  19. void PlaySound1 (short, short);
  20. void PlaySound2 (short, short);
  21. pascal void ExternalCallBack (SndChannelPtr, SndCommand);
  22. pascal void ExternalCallBack2 (SndChannelPtr, SndCommand);
  23. void LoadAllSounds (void);
  24. OSErr LoadBufferSounds (void);
  25. OSErr DumpBufferSounds (void);
  26. OSErr OpenSoundChannel (void);
  27. OSErr CloseSoundChannel (void);
  28.  
  29.  
  30. SndCallBackUPP        externalCallBackUPP, externalCallBackUPP2;
  31. SndChannelPtr        externalChannel, externalChannel2;
  32. Ptr                    theSoundData[kMaxSounds];
  33. short                externalPriority, externalPriority2;
  34. Boolean                channelOpen, soundOn;
  35.  
  36.  
  37. //==============================================================  Functions
  38. //--------------------------------------------------------------  PlaySound1
  39.  
  40. void PlaySound1 (short soundID, short priority)
  41. {
  42.     SndCommand    theCommand;
  43.     OSErr        theErr;
  44.     
  45.     theCommand.cmd = flushCmd;
  46.     theCommand.param1 = 0;
  47.     theCommand.param2 = 0L;
  48.     theErr = SndDoImmediate(externalChannel, &theCommand);
  49.     
  50.     theCommand.cmd = quietCmd;
  51.     theCommand.param1 = 0;
  52.     theCommand.param2 = 0L;
  53.     theErr = SndDoImmediate(externalChannel, &theCommand);
  54.     
  55.     externalPriority = priority;
  56.     
  57.     theCommand.cmd = bufferCmd;
  58.     theCommand.param1 = 0;
  59.     theCommand.param2 = (long)(theSoundData[soundID]);
  60.     theErr = SndDoImmediate(externalChannel, &theCommand);
  61.     
  62.     theCommand.cmd = callBackCmd;
  63.     theCommand.param1 = kSoundDone;
  64.     theCommand.param2 = SetCurrentA5();
  65.     theErr = SndDoCommand(externalChannel, &theCommand, TRUE);
  66. }
  67.  
  68. //--------------------------------------------------------------  PlaySound2
  69.  
  70. void PlaySound2 (short soundID, short priority)
  71. {
  72.     SndCommand    theCommand;
  73.     OSErr        theErr;
  74.     
  75.     theCommand.cmd = flushCmd;
  76.     theCommand.param1 = 0;
  77.     theCommand.param2 = 0L;
  78.     theErr = SndDoImmediate(externalChannel2, &theCommand);
  79.     
  80.     theCommand.cmd = quietCmd;
  81.     theCommand.param1 = 0;
  82.     theCommand.param2 = 0L;
  83.     theErr = SndDoImmediate(externalChannel2, &theCommand);
  84.     
  85.     externalPriority2 = priority;
  86.     
  87.     theCommand.cmd = bufferCmd;
  88.     theCommand.param1 = 0;
  89.     theCommand.param2 = (long)(theSoundData[soundID]);
  90.     theErr = SndDoImmediate(externalChannel2, &theCommand);
  91.     
  92.     theCommand.cmd = callBackCmd;
  93.     theCommand.param1 = kSoundDone2;
  94.     theCommand.param2 = SetCurrentA5();
  95.     theErr = SndDoCommand(externalChannel2, &theCommand, TRUE);
  96. }
  97.  
  98. //--------------------------------------------------------  PlayExternalSound
  99.  
  100. void PlayExternalSound (short soundID, short priority)
  101. {
  102.     if ((soundID >= 0) && (soundID < kMaxSounds))
  103.     {
  104.         if (soundOn)
  105.         {
  106.             if (externalPriority < externalPriority2)
  107.             {
  108.                 if (priority >= externalPriority)
  109.                     PlaySound1(soundID, priority);
  110.             }
  111.             else
  112.             {
  113.                 if (priority >= externalPriority2)
  114.                     PlaySound2(soundID, priority);
  115.             }
  116.         }
  117.     }
  118. }
  119.  
  120. //--------------------------------------------------------  ExternalCallBack
  121.  
  122. RoutineDescriptor ExternalCallBackRD = 
  123.         BUILD_ROUTINE_DESCRIPTOR(uppFilePlayCompletionProcInfo, ExternalCallBack);
  124.  
  125. pascal void ExternalCallBack (SndChannelPtr theChannel, SndCommand theCommand)
  126. {
  127.     long        thisA5, gameA5;
  128.     
  129.     if (theCommand.param1 == kSoundDone)
  130.     {
  131.         gameA5 = theCommand.param2;
  132.         thisA5 = SetA5(gameA5);
  133.         
  134.         externalPriority = 0;
  135.         
  136.         thisA5 = SetA5(thisA5);
  137.     }
  138. }
  139.  
  140. //--------------------------------------------------------  ExternalCallBack2
  141.  
  142. RoutineDescriptor ExternalCallBackRD2 = 
  143.         BUILD_ROUTINE_DESCRIPTOR(uppFilePlayCompletionProcInfo, ExternalCallBack2);
  144.  
  145. pascal void ExternalCallBack2 (SndChannelPtr theChannel, SndCommand theCommand)
  146. {
  147.     long        thisA5, gameA5;
  148.     
  149.     if (theCommand.param1 == kSoundDone2)
  150.     {
  151.         gameA5 = theCommand.param2;
  152.         thisA5 = SetA5(gameA5);
  153.         
  154.         externalPriority2 = 0;
  155.         
  156.         thisA5 = SetA5(thisA5);
  157.     }
  158. }
  159.  
  160. //--------------------------------------------------------  LoadBufferSounds
  161.  
  162. OSErr LoadBufferSounds (void)
  163. {
  164.     Handle        theSound;
  165.     long        soundDataSize;
  166.     OSErr        theErr;
  167.     short        i;
  168.     
  169.     theErr = noErr;
  170.     
  171.     for (i = 0; i < kMaxSounds; i++)
  172.     {
  173.         theSound = GetResource('snd ', i + kBaseBufferSoundID);
  174.         if (theSound == 0L)
  175.             return (ResError());
  176.         
  177.         HLock(theSound);
  178.         soundDataSize = GetHandleSize(theSound) - 20L;
  179.         HUnlock(theSound);
  180.         
  181.         theSoundData[i] = NewPtr(soundDataSize);
  182.         if (theSoundData[i] == 0L)
  183.             return (ResError());
  184.         HLock(theSound);
  185.         BlockMove((Ptr)(*theSound + 20L), theSoundData[i], soundDataSize);
  186.         HUnlock(theSound);
  187.         ReleaseResource(theSound);
  188.     }
  189.     
  190.     return (theErr);
  191. }
  192.  
  193. //--------------------------------------------------------  DumpBufferSounds
  194.  
  195. OSErr DumpBufferSounds (void)
  196. {
  197.     OSErr        theErr;
  198.     short        i;
  199.     
  200.     theErr = noErr;
  201.     
  202.     for (i = 0; i < kMaxSounds; i++)
  203.     {
  204.         if (theSoundData[i] != 0L)
  205.             DisposPtr(theSoundData[i]);
  206.         theSoundData[i] = 0L;
  207.     }
  208.     
  209.     return (theErr);
  210. }
  211.  
  212. //--------------------------------------------------------  OpenSoundChannel
  213.  
  214. OSErr OpenSoundChannel (void)
  215. {
  216.     OSErr        theErr;
  217.     
  218.     #if USESROUTINEDESCRIPTORS
  219.         externalCallBackUPP = &ExternalCallBackRD;
  220.         externalCallBackUPP2 = &ExternalCallBackRD2;
  221.     #else
  222.         externalCallBackUPP = (SndCallBackUPP) &ExternalCallBack;
  223.         externalCallBackUPP2 = (SndCallBackUPP) &ExternalCallBack2;
  224.     #endif
  225.     
  226.     theErr = noErr;
  227.     
  228.     if (channelOpen)
  229.         return (theErr);
  230.     
  231.     externalChannel = 0L;
  232.     theErr = SndNewChannel(&externalChannel, 
  233.             sampledSynth, initNoInterp + initMono, 
  234.             (SndCallBackUPP)externalCallBackUPP);
  235.     if (theErr == noErr)
  236.         channelOpen = TRUE;
  237.     
  238.     externalChannel2 = 0L;
  239.     theErr = SndNewChannel(&externalChannel2, 
  240.             sampledSynth, initNoInterp + initMono, 
  241.             (SndCallBackUPP)externalCallBackUPP2);
  242.     if (theErr == noErr)
  243.         channelOpen = TRUE;
  244.     
  245.     return (theErr);
  246. }
  247.  
  248. //--------------------------------------------------------  CloseSoundChannel
  249.  
  250. OSErr CloseSoundChannel (void)
  251.  
  252. {
  253.     OSErr        theErr;
  254.     
  255.     theErr = noErr;
  256.     
  257.     if (!channelOpen)
  258.         return (theErr);
  259.     
  260.     if (externalChannel != 0L)
  261.         theErr = SndDisposeChannel(externalChannel, TRUE);
  262.     externalChannel = 0L;
  263.     
  264.     if (externalChannel2 != 0L)
  265.         theErr = SndDisposeChannel(externalChannel2, TRUE);
  266.     externalChannel2 = 0L;
  267.     
  268.     if (theErr == noErr)
  269.         channelOpen = FALSE;
  270.     
  271.     return (theErr);
  272. }
  273.  
  274. //--------------------------------------------------------  InitSound
  275.  
  276. void InitSound (void)
  277. {
  278.     OSErr        theErr;
  279.     
  280.     soundOn = TRUE;
  281.     
  282.     externalChannel = 0L;
  283.     externalChannel2 = 0L;
  284.     externalPriority = 0;
  285.     externalPriority2 = 0;
  286.     
  287.     theErr = LoadBufferSounds();
  288.     if (theErr != noErr)
  289.         RedAlert("\pFailed Loading Sounds");
  290.     
  291.     theErr = OpenSoundChannel();
  292.     if (theErr != noErr)
  293.         RedAlert("\pFailed To Open Sound Channels");
  294. }
  295.  
  296. //--------------------------------------------------------  KillSound
  297.  
  298. void KillSound (void)
  299. {
  300.     OSErr        theErr;
  301.     
  302.     theErr = DumpBufferSounds();
  303.     theErr = CloseSoundChannel();
  304. }
  305.  
  306.